'''# built_in'''
import os
import json

'''# custom'''
from common import GSDSTORE 


class InitializationParameter:

    def __init__(self) -> None:
        GSDSTORE['START'] = {}

    def __call__(self, state):
        try:
            if state == "static":
                '''自定义文件静态配置执行'''
                try:
                    with open('./parameters.json', 'r', encoding="utf-8") as f:
                        parameter = json.load(f)
                except Exception as error:
                    print("No necessary documents：parameters.json ! ")
                else:
                    GSDSTORE['START']['packetPath'] = parameter['packetPath']
                    GSDSTORE['START']['testCaseFile'] = parameter['testCaseFile']
                    GSDSTORE['START']['browser'] = parameter['testEnvironment']['browser']
                    GSDSTORE['START']['url'] = parameter['testEnvironment']['url']
                    GSDSTORE['START']['implicitlyWait'] = parameter['testEnvironment']['implicitlyWait']
                    GSDSTORE['START']['user'] = parameter['testUser']['user']
                    GSDSTORE['START']['password'] = parameter['testUser']['password']
                    GSDSTORE['START']['pluginName'] = parameter['auxiliaryPlugIn']['pluginName']
                    GSDSTORE['START']['interfaceSwitch'] = parameter['auxiliaryFunction']['interfaceSwitch']
                    GSDSTORE['START']['codeSwitch'] = parameter['auxiliaryFunction']['codeSwitch']
                    GSDSTORE['START']['retrySwitch'] = parameter['auxiliaryFunction']['retrySwitch']
            else:
                raise Exception
        
        except Exception as error:
            '''可视化窗口动态配置执行'''
            from GUI.main import visibleWindow
            try:
                visibleWindow()()
            except Exception as error:
                print("Window event exception ! ")
        
        finally:
            '''全局数据 - 工作目录路径信息'''
            '''
                interfacePath
                casesPath
                elementPath
                auxiliaryPath
                resultPaht
            '''
            if os.path.exists(GSDSTORE['START']['packetPath']):
                for folder in os.listdir(GSDSTORE['START']['packetPath']):
                    tranKey = folder[:folder.find('_')]
                    if folder[:folder.find('_') + 1] in ['interface_', 'cases_', 'element_', 'auxiliary_']:
                        GSDSTORE['WORKPATH'][f'{tranKey}Path'] = folder
                else:
                    GSDSTORE['WORKPATH']['resultPaht'] = f"{GSDSTORE['WORKPATH']['ROOT']}\\result\\excel"

                for key in ['interfacePath', 'casesPath', 'elementPath']:
                    if not key in GSDSTORE['WORKPATH'].keys():
                        print(f"所指定的用例数据包目录中，缺少必要的文件夹目录 ！{key}"); raise Exception

                if False in [True if os.path.exists(filePath) else False for filePath in GSDSTORE['START']['testCaseFile']]:
                    print(f"所指定的用例步骤文件列表中，存在非法项：{GSDSTORE['START']['testCaseFile']}"); raise Exception
            else: 
                print(f"所指定的用例数据包目录不存在：{GSDSTORE['START']['packetPath']}"); raise Exception